﻿/*********************************************
 创建者：  george
 创建时间：2017-06-21
 功能说明：KM顾客满意度调研
 ********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using Activity.Data;
using Activity.Model;

namespace Activity.API
{
    /// <summary>
    /// KM顾客满意度调研
    /// </summary>
    public class QuestionnaireHandler
    {
        /// <summary>
        /// 结束时间
        /// </summary>
        private static readonly DateTime endDate = new DateTime(2017, 06, 26, 23, 59, 59);

        /// <summary>
        /// 开始时间
        /// </summary>
        private static readonly DateTime beginDate = new DateTime(2017, 06, 23, 21, 30, 0);

        /// <summary>
        /// 测试
        /// </summary>
        static readonly string isDebug = WebConfigurationManager.AppSettings["isDebug"];

        static readonly string THQID = WebConfigurationManager.AppSettings["hk-THQID"];

        /// <summary>
        /// 返回现金券
        /// </summary>
        /// <returns></returns>
        public Messages Save(wj_questionnaire param)
        {
            if (string.IsNullOrWhiteSpace(param.openId))
                return new Messages {IsSuc = false, Code = -1, Message = "无效的OPENID"};
            if (DateTime.Now < beginDate && isDebug.ToLower() == "false")
                return new Messages { IsSuc = false, Code = -2, Message = "亲，活动还没开始呢" };
            if (DateTime.Now >= endDate && isDebug.ToLower() == "false")
                return new Messages { IsSuc = false, Code = -3, Message = "亲，活动已经结束了" };
            var dc = new ActivityEntities();
            if (dc.wj_questionnaire.Any(t => t.openId == param.openId))
                return new Messages { IsSuc = false, Code = -4, Message = "请不要重复提交" };
            var hkCode = GetCashCode(dc);
            if(hkCode=="000000") return new Messages { IsSuc = false, Code = -5, Message = "获取现金券错误"};
            param.createDate = DateTime.Now;
            param.cashCode = hkCode;
            dc.wj_questionnaire.Add(param);
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new Messages {IsSuc = false, Code = -1, Message = ex.Message};
            }
            return new Messages {IsSuc = true, Code = 0, Message = hkCode};
        }

        private  static object obj=new object();
        /// <summary>
        /// 获取现金券
        /// </summary>
        /// <returns></returns>
        public string GetCashCode(ActivityEntities dc)
        {
            var query = string.Empty;
            //if (isDebug.ToLower() == "true") return "";
            try
            {
                if (dc == null) dc = new ActivityEntities();
                var hk = new HKEntities();
                lock (obj)
                {
                    var exLogList = dc.wj_questionnaire.Select(t => t.cashCode).ToList();
                    var exCode =
                        hk.SD_POS_THQDetail.Where(t => t.THQID == THQID && t.THQStatus == 0)
                            .Select(t => t.THQNo)
                            .ToList();
                    query = exCode.Except(exLogList).FirstOrDefault();
                    var modify = hk.SD_POS_THQDetail.FirstOrDefault(t => t.THQNo == query && t.THQID == THQID);
                    if (modify != null)
                        modify.THQStatus = 1;

                    hk.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error("获取现金券出错：" + ex);
                return "000000";
            }
            return query ?? "000000";
        }

        /// <summary>
        /// 返回结果
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public wj_questionnaire GetDetail(string openId)
        {
            var dc = new ActivityEntities();
            var query = dc.wj_questionnaire.FirstOrDefault(t => t.openId == openId);
            return query;
        }

        public List<vm_question_result> GetResult()
        {
            var dc = new ActivityEntities();
            var result = dc.vm_question_result.OrderByDescending(t => t.Memo).ThenByDescending(t => t.num).ToList();
            return result;
        }
    }

}
